package com.bona.service.system;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bona.dao.common.BaseDao;
import com.bona.dao.system.SysRoleDao;
import com.bona.dao.system.SysRoleFunctionDao;
import com.bona.dao.system.SysRoleOrgDao;
import com.bona.dao.system.SysRoleUserDao;
import com.bona.model.system.SysRoleFunctionModel;
import com.bona.model.system.SysRoleModel;
import com.bona.model.system.SysRoleOrgModel;
import com.bona.model.system.SysRoleUserModel;
import com.bona.service.common.BaseService;
import com.bona.util.common.StringUtil;
import com.bona.util.common.UUIDGenerator;

/**
 * 
 * <br>
 * <b>功能：</b>SysRoleService<br>
 * <b>作者：</b>tbzeng<br>
 * <b>日期：</b> Dec 9, 2015 <br>
 * <b>版权所有：<b>版权所有(C) 2015，tbzeng<br>
 */
@Service("roleService")
@Transactional
public class SysRoleService extends BaseService {
	
	private final static Logger logger = Logger.getLogger(SysRoleService.class);
	
	@Autowired
	private SysRoleDao roleDao;
	@Autowired
	private SysRoleUserDao roleUserDao;
	@Autowired
	private SysRoleOrgDao roleOrgDao;
	@Autowired
	private SysRoleFunctionDao roleFunctionDao;
	
	/**
	 * 添加或更新用户
	 * @param model
	 */
	public void add(SysRoleModel model) {
		
		String id = model.getId();
		if (StringUtil.isEmpty(id)) {	// 新增角色
			model.setId(UUIDGenerator.generate());
			roleDao.add(model);
		} else {	// 更新角色
			SysRoleModel roleModel = roleDao.query(new SysRoleModel(id));
			roleModel.setRolecode(model.getRolecode());
			roleModel.setRolename(model.getRolename());
			roleModel.setUpdateBy(model.getUpdateBy());
			roleModel.setUpdateName(model.getUpdateName());
			roleModel.setUpdateDate(model.getUpdateDate());
			roleDao.update(roleModel);
		}
	}
	
	/**
	 * 查询机构关联的角色
	 * @param userId
	 * @return
	 */
	public List<SysRoleModel> queryRoleByOrgId(String orgId) {
		
		List<SysRoleModel> models = roleDao.queryRoleByOrgId(orgId);
		return models;
	}
	
	/**
	 * 获取角色菜单关联关系
	 * @param roleId
	 * @param funcId
	 * @return
	 */
	public SysRoleFunctionModel queryRoleFunction(SysRoleFunctionModel model) {
		return roleFunctionDao.query(model);
	}
	
	/** 获取菜单关联的角色菜单关联关系
	 * @param funcId
	 * @return
	 */
	public List<SysRoleFunctionModel> queryRoleFunctionByFuncId(String funcId) {
		
		List<SysRoleFunctionModel> models = roleDao.queryRoleFunctionByFuncId(funcId);
		return models;
	}
	
	/**
	 * 获取角色下的所有用户编号
	 * @param roleId
	 * @return
	 */
	public Integer queryRoleUserCount(SysRoleUserModel model) {
		return roleUserDao.queryByCount(model);
	}
	
	/** 获取角色下所关联的菜单编号
	 * @param roleId
	 * @return
	 */
	public List<String> queryFunctionIdByRoleId(@Param("roleId")String roleId) {
		
		List<String> roleIds = roleDao.queryFunctionIdByRoleId(roleId);
		return roleIds;
	}
	
	/**
	 * 删除角色关联的用户
	 * @param roleId
	 * @return
	 */
	public Integer deleteRoleUserByRoleId(String roleId) {
		
		return roleDao.deleteRoleUserByRoleId(roleId);
	}
	
	/**
	 * 添加角色机构关联关系
	 * @param models
	 * @return
	 */
	public Integer addRoleOrg(SysRoleOrgModel... models) {
		
		return roleDao.addRoleOrg(Arrays.asList(models));
	}
	
	/**
	 * 删除角色信息
	 * @param roleId
	 */
	public void delete(String roleId) {
		
		// 删除角色信息
		int roleCnt = roleDao.delete(new SysRoleModel(roleId));
		// 解除角色机构关联关系
		SysRoleOrgModel roleOrgModel = new SysRoleOrgModel();
		roleOrgModel.setRoleId(roleId);
		int roleUserCnt = roleOrgDao.delete(roleOrgModel);
		// 解除角色菜单关联关系
		SysRoleFunctionModel roleFunctionModel = new SysRoleFunctionModel();
		roleFunctionModel.setRoleid(roleId);
		int roleFunctionCnt = roleDao.delete(roleFunctionModel);
		logger.info("删除角色机构["+roleUserCnt+"],角色菜单["+roleFunctionCnt+"],角色["+roleCnt+"]");
	}
	
	/**
	 * 更新当前角色菜单关联关系
	 * @param roleId
	 * @param funcId
	 */
	public void addFunctionToRole(String roleId, String funcIds) {
		
		if (StringUtil.isEmpty(roleId) || StringUtil.isEmpty(funcIds)) return;
		SysRoleFunctionModel roleFunctionModel = new SysRoleFunctionModel();
		roleFunctionModel.setRoleid(roleId);
		roleFunctionDao.delete(roleFunctionModel);	// 删除角色菜单的关联关系
		for (String funcId: funcIds.split(",")) {
			roleFunctionModel = new SysRoleFunctionModel();
			roleFunctionModel.setId(UUIDGenerator.generate());
			roleFunctionModel.setRoleid(roleId);
			roleFunctionModel.setFunctionid(funcId);
			roleFunctionDao.add(roleFunctionModel);
		}
	}
	
	/**
	 * 更新角色机构关联关系
	 * @param orgId
	 * @param asList
	 */
	public void updateRoleOrg(String orgId, List<String> roleIdList) {
		
		if (roleIdList==null || roleIdList.size()<=0) return;
		// 删除角色机构关联关系
		int delCnt = roleDao.deleteRoleOrgByOrgId(orgId);
		List<SysRoleOrgModel> models = new ArrayList<SysRoleOrgModel>();
		for (String roleId: roleIdList) {
			SysRoleOrgModel model = new SysRoleOrgModel();
			model.setId(UUIDGenerator.generate());
			model.setRoleId(roleId);
			model.setOrgId(orgId);
			models.add(model);
		}
		// 添加角色机构关联关系
		int addCnt = roleDao.addRoleOrg(models);
		logger.info("更新角色机构关联关系，删除["+delCnt+"],新增["+addCnt+"]");
	}
	
	/**
	 * 删除角色下的用户角色关联关系
	 * @param roleId
	 * @param userIds
	 * @return
	 */
	public void deleteRoleUser(SysRoleUserModel model) {
		
		String roleids = model.getRoleid();
		String userids = model.getUserid();
		if (StringUtil.isEmpty(roleids) || StringUtil.isEmpty(userids)) 
			return ;
		for (String userid: userids.split(",")) {
			for (String roleid: roleids.split(",")) {
				model = new SysRoleUserModel(roleid, userid);
				roleUserDao.delete(model);
			}
		}
	}
	
	/**
	 * 绑定用户到当前角色
	 * @param roleId
	 * @param userIds
	 */
	public void addUserToRole(String roleId, String userIds) {
		
		if (StringUtil.isEmpty(roleId) || StringUtil.isEmpty(userIds)) return;
		String[] userIdArr = userIds.split(",");
		for (String userId: userIdArr) {
			SysRoleUserModel roleUserModel = new SysRoleUserModel();
			roleUserModel.setId(UUIDGenerator.generate());
			roleUserModel.setRoleid(roleId);
			roleUserModel.setUserid(userId);
			roleUserDao.add(roleUserModel);
		}
	}
	
	@Override
	public BaseDao getDao() {
		return roleDao;
	}

}
